import { createApp } from 'vue'
import Message from './Message.vue'
export type MessageType = 'success' | 'error' | 'default'

const createMessage = (message: string, type: MessageType, timeout = 2000) => {
  const messageInstance = createApp(Message, {
    message,
    type,
    onCloseMessage:()=>{
      messageInstance.unmount()
      if(document.body.contains(mountNode)) {
        document.body.removeChild(mountNode)
      }
    }
  })
  const mountNode = document.createElement('div')
  document.body.appendChild(mountNode)
  messageInstance.mount(mountNode)
  
  // let timer: number | undefined
  // timer =
   setTimeout(() => {
    messageInstance.unmount()
    if(document.body.contains(mountNode)) {
      document.body.removeChild(mountNode)
    }
    // clearTimeout(timer)
  }, timeout)
}

export default createMessage